//
//  ZLRefreshView.swift
//  刷新控件
//
//  Created by zhanglongtao on 17/1/3.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import UIKit

class ZLRefreshView: UIView {

    /**
     iOS系统中uiview封装的动画
     -默认顺时针旋转
     - 就近原则
     实现同方向旋转减去一个很小的数字
     如果想实现360°的旋转，需要核心动画CABaseAnimation
     */
    var refreshSate: ZLRefreshState = .Normal {
        
        didSet{
            
            switch refreshSate {
            case .Normal:
             
                tipLabel?.text = "下拉开始刷新"
                tipIcon?.isHidden = false
                indicator?.isHidden = true
                indicator?.stopAnimating()
                UIView.animate(withDuration: 0.25, animations: {
                    
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
                
                
            case .Pulling:
                tipLabel?.text = "放手开始刷新"
                tipIcon?.isHidden = false
                indicator?.isHidden = true
                indicator?.stopAnimating()
                UIView.animate(withDuration: 0.25, animations: { 
                    
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.0001))
                })
                
                
            case .WillRefresh:
                tipLabel?.text = "正在刷新中"
                tipIcon?.isHidden = true
                indicator?.isHidden = false
                indicator?.startAnimating()
                
            }
        }
    }
    
    /// 父视图的高度
    var parentViewHeight: CGFloat = 0
    
    //指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    //提示图标
    @IBOutlet weak var tipIcon: UIImageView?
    //提示标签
    @IBOutlet weak var tipLabel: UILabel?
    
    class func refreshView() -> ZLRefreshView {
        
        let nib = UINib(nibName: "ZLMeiTuanRefreshControl", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! ZLRefreshView
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
